/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void Inorder(TreeNode* root,stack<int> &tmp,int k){
        if(root!=NULL){
            Inorder(root->left,tmp,k);
            if(tmp.size()<k)
                tmp.push(root->val);
            else
                return;
            Inorder(root->right,tmp,k);
        }
    }
    int kthSmallest(TreeNode* root, int k) {
        stack<int> tmp;
        Inorder(root,tmp,k);
        int res=tmp.top();
        return res;
    }
};
